package com.stars.empoly.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.ViewGroup
import com.hwangjr.rxbus.annotation.Subscribe
import com.hwangjr.rxbus.annotation.Tag
import com.qmuiteam.qmui.kotlin.onClick
import com.stars.empoly.base.MBFragment
import com.stars.empoly.config.ConfigRx
import com.stars.empoly.databinding.FragmentHomeMeBinding
import com.stars.empoly.utils.CleanDataUtils
import com.stars.empoly.vmodel.HomeMeVModel
import com.tencent.mmkv.MMKV
import java.util.*

/**
 *on 2022/9/22
 */
class HomeMeFragemnt: MBFragment<HomeMeVModel, FragmentHomeMeBinding>() {


    override fun init(savedInstanceState: Bundle?) {
        super.init(savedInstanceState)
         vm.getUserInfo()
        vm.getNotMsg()


        try {
            vm.cacheSize.value = CleanDataUtils.getTotalCacheSize(Objects.requireNonNull(context()))
        } catch (e: Exception) {
            e.printStackTrace()
        }
        var cityName= MMKV.defaultMMKV().getString("city", "")
        binding.tvCity.text=cityName

    }

    override fun listeners() {
        super.listeners()
        binding.llClearSize.onClick {
            CleanDataUtils.clearAllCache(Objects.requireNonNull(context()))
            vm.cacheSize.value = CleanDataUtils.getTotalCacheSize(Objects.requireNonNull(context()))
            toast("缓存清理成功！")


        }
    }

    override fun vmClass(): Class<HomeMeVModel> {
        return HomeMeVModel::class.java
    }
    override fun binding(inflater: LayoutInflater, container: ViewGroup?): FragmentHomeMeBinding {
        return FragmentHomeMeBinding.inflate(layoutInflater)
    }

    override fun onRefresh() {
        super.onRefresh()
        vm.getUserInfo()
    }

    override fun title(): String? {
        return null
    }
    override val isRxbus: Boolean
        get() = true


    @Subscribe(tags = [Tag(ConfigRx.MSG_RED)])
    fun setRefreshMsg(position: String) {
        //刷新未读消息数量
        vm.getNotMsg()

    }
    @Subscribe(tags = [Tag(ConfigRx.REFRESH_USER)])
    fun setRefreshUser(type: String) {
        //刷新用户信息
        vm.getUserInfo()

    }
}
